//
//  mainHongBaoVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/6/4.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "mainHongBaoVC.h"
#import "hongBaoCell.h"
#import "hongBaoModel.h"
@interface mainHongBaoVC ()


@end

@implementation mainHongBaoVC
- (void)viewDidLoad {
    [super viewDidLoad];
    [helpPublic setCancleTitle:self.navigationItem.backBarButtonItem];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"我的红包";
    // 添加所有子控制器
    [self setUpAllViewController];
    // 推荐方式
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight, CGFloat *titleWidth) {
        // 设置标题字体
        *titleFont = [UIFont systemFontOfSize:15];
        *titleHeight = 35;
        *selColor = [UIColor custom_color:@"#A91E23"];
    }];
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor, BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = [UIColor custom_color:@"#A91E23"];
    }];
    __weak mainHongBaoVC *ws = self;
    [self setUpContentViewFrame:^(UIView *contentView) {
        contentView.frame = ws.view.frame;
    }];
    
    UIView *grayLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
    grayLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:grayLine];
}
- (void)setUpAllViewController {
    hongBaoVC *VC1 = [hongBaoVC new];
    hongBaoVC *VC2 = [hongBaoVC new];
    hongBaoVC *VC3 = [hongBaoVC new];
    
    VC1.title = @"未使用";
    VC2.title = @"已使用";
    VC3.title = @"已过期";
    
    [self addChildViewController:VC1];
    [self addChildViewController:VC2];
    [self addChildViewController:VC3];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

@implementation hongBaoVC
- (void)viewDidLoad {
    
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 35);
    self.tableView = [[UITableView alloc]initWithFrame:frame];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        //        if (iPhoneX) {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 88, 0);
        //        }else {
        //            self.detialView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        //        }
        //        self.detialView.scrollIndicatorInsets = self.detialView.contentInset;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.page = 1;
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self confingureTableView];
    
    [self hongBaoRequest];
}

// MARK: - 下拉刷新
- (void)addRefreshHeader {
    [super addRefreshHeader];
}
- (void)addRefreshFooter {
    [super addRefreshFooter];
}

- (void)loadNew
{
    self.page = 1;
    [self hongBaoRequest];
}

- (void)loadMore
{
    if (!self.hongBaoList.count) {
        [self endFooterRefresh];
        return;
    }
    self.page = self.page + 1;
    [self hongBaoRequest];
}
- (void)confingureTableView {
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([hongBaoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([hongBaoCell class])];
}
- (void)handleResponseData:(NSDictionary *)responseObject
{
    NSMutableArray *modelArray =  [hongBaoModel mj_objectArrayWithKeyValuesArray:responseObject[@"userHongbaoViews"]];
    if (self.page == 1) {
        [self.hongBaoList removeAllObjects];
    }
    if (modelArray.count > 0) {
        [self.hongBaoList addObjectsFromArray:(NSArray *)modelArray];
        if (modelArray.count < 10) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    }else {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}

#pragma mark --- uitableViewDelegate and dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.hongBaoList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    hongBaoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([hongBaoCell class])];
    cell.model = [self.hongBaoList objectAtIndex:indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.title isEqualToString:@"未使用"]) {
        [[helpPublic getCurrentViewController:self].navigationController popToRootViewControllerAnimated:YES];
    }
}
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
//    grayView.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    return grayView;
//}

- (void)hongBaoRequest {
    NSString *status;
    if ([self.title isEqualToString:@"已使用"]) {
        status = @"1";
    }else if ([self.title isEqualToString:@"未使用"]) {
        status = @"0";
    }else if ([self.title isEqualToString:@"已过期"]) {
        status = @"2";
    }
    NSDictionary *dic = @{
                          @"pager.pageNumber":@(self.page),
                          @"pager.pageSize":@10,
                          @"token":USERTOKEN,
                          @"status":status
                          };
    __weak typeof(self) weakSelf = self;
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] mainHongBao] Dic:dic SuccessBlock:^(id responseObject) {
        [weakSelf endRefreshing];
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [weakSelf handleResponseData:responseObject];
        }else {
//            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [weakSelf endRefreshing];
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
#pragma mark --- getter
- (NSMutableArray *)hongBaoList {
    if (!_hongBaoList) {
        _hongBaoList = [NSMutableArray array];
    }
    return _hongBaoList;
}

@end
